package com.xxhui.guidepagedemo.Fragment;


import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;

import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.xxhui.guidepagedemo.R;
import com.xxhui.guidepagedemo.ViewPage.ScaleStackViewPage;


public class ScaleStackViewPageFragment extends Fragment {

    private ScaleStackViewPage viewPage;

    private int[] imageRes = {R.mipmap.a, R.mipmap.b, R.mipmap.c, R.mipmap.d};

    private Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity().getApplicationContext();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_scale_stack_view_page, container, false);
        viewPage = (ScaleStackViewPage) v.findViewById(R.id.ScaleStackViewPage);
        viewPage.setAdapter(new ScaleStackPageAdapter());
        return v;
    }


    class ScaleStackPageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageRes.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }


        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(imageRes[position]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            //ImageView.ScaleType.CENTER_CROP:按比例扩大图片的size居中显示，使得图片长(宽)等于或大于View的长(宽)
            container.addView(imageView);
            viewPage.updatePageMap(position,imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {//必须重写，不然报错
            container.removeView((View) object);
        }
    }
}
